package org.example.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.Person;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器 (可以用于登录校验)
 * <p>
 * 例如：针对简单系统，可以在注册配置中针对公共资源进行放行。
 * 特殊资源进行登录拦截，用于判断是否登录。
 * @Author: WangShengJie
 * @Date: 2025/4/21
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    private static final  ThreadLocal<Person> THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 前置拦截器（处理器处理前调用）
     * @param request 请求
     * @param response  响应
     * @param handler 控制器
     * @return boolean
     * @throws Exception 异常
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return HandlerInterceptor.super.preHandle(request, response, handler);
        System.out.println(handler);
        System.out.println("前置拦截器");
        return true;
    }

    /**
     * 后置拦截器（处理器处理后调用）
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        System.out.println("后置拦截器");
    }

    /**
     * 在整个请求完成之后被调用，即视图渲染结束之后。
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
